//
//  ELBasePupupView.m
//  SportRing
//
//  Created by elwin on 10/29/14.
//  Copyright (c) 2014 HotOne. All rights reserved.
//

#import "ELBasePupupView.h"
#import "SRAppDelegate.h"
#import <QuartzCore/QuartzCore.h>


@interface ELBasePupupView()
- (void)initIt;
- (NSInteger)getKeyBoardHeight:(NSNotification *)notification;
- (void)keyboardWillShow:(NSNotification *)notification;
+ (SRAppDelegate*)getAppDelegate;

- (void)fadeIn;
- (void)fadeOut;
- (void)fadeOutWithoutAnimation;
- (void)showWithoutAnimation;
- (void)dismiss;

@end

@implementation ELBasePupupView

@synthesize touchToDismiss = _touchToDismiss;
@synthesize dismissAction = _dismissAction;
@synthesize confirmAction = _confirmAction;

+ (SRAppDelegate*)getAppDelegate
{
    return (SRAppDelegate*)[[UIApplication sharedApplication] delegate];
}

+ (UIWindow*)getMainWindow
{
    SRAppDelegate* app = [ELBasePupupView getAppDelegate];
    return app.window;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initIt];
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self initIt];
}

- (void)initIt
{
    incressedHeight = 0.0f;
    disableUserInterfaced = NO;
    self.touchToDismiss = YES;
    self.layer.cornerRadius = 3.0f;
    self.clipsToBounds = TRUE;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}

#pragma mark - animations
- (void)fadeInFromBottom:(UIView *)view
{
    self.frame = CGRectMake(0, view.frame.size.height, self.frame.size.width, self.frame.size.height);
    self.alpha = 0;
    [UIView animateWithDuration:.25 animations:^{
        self.alpha = 1;
        self.frame = CGRectMake(0, view.frame.size.height - self.frame.size.height, self.frame.size.width, self.frame.size.height);
    } completion:^(BOOL finished) {
 
    }];
    
}

- (void)fadeOutToBottom:(UIView *)view;
{
    self.frame = CGRectMake(0, view.frame.size.height - self.frame.size.height, self.frame.size.width, self.frame.size.height);
    self.alpha = 0;
    [UIView animateWithDuration:.25 animations:^{
        self.alpha = 1;
        self.frame = CGRectMake(0, view.frame.size.height, self.frame.size.width, self.frame.size.height);

    } completion:^(BOOL finished) {
        if (finished) {
            [_overlayView removeFromSuperview];
            [self removeFromSuperview];
        }
    }];
    
}

- (void)fadeIn
{
    self.transform = CGAffineTransformMakeScale(1.3, 1.3);
    self.alpha = 0;
    [UIView animateWithDuration:.25 animations:^{
        self.alpha = 1;
        self.transform = CGAffineTransformMakeScale(1, 1);
    }];

}
- (void)fadeOut
{
    [UIView animateWithDuration:.25 animations:^{
        self.transform = CGAffineTransformMakeScale(1.3, 1.3);
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        if (finished) {
            [_overlayView removeFromSuperview];
            [self removeFromSuperview];
        }
    }];
}
- (void)fadeOutWithoutAnimation
{
    self.alpha = 0.0;
    [_overlayView removeFromSuperview];
    [self removeFromSuperview];
}

- (void)showInView:(UIView *)pView withOverlayView:(BOOL)overLay{
    _overlayView = [[UIControl alloc] initWithFrame:pView.frame];
    _overlayView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.5];
    if (overLay) {
        [pView addSubview:_overlayView];
    }
    if(self.touchToDismiss)
    {
        [_overlayView addTarget:self
                         action:@selector(dismiss)
               forControlEvents:UIControlEventTouchUpInside];
        [pView addSubview:_overlayView];
    }
    [pView addSubview:self];
    [pView bringSubviewToFront:self];
    
    self.center = CGPointMake(pView.bounds.size.width/2.0f,pView.bounds.size.height/2.0f);
    
    [self fadeIn];
}

- (void)showInView:(UIView*)pView
{
    [self showInView:pView withOverlayView:NO];
    
}

- (void)showInViewAtBottom:(UIView*)pView
{
    _overlayView = [[UIControl alloc] initWithFrame:pView.frame];
    _overlayView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.5];
    
    if(self.touchToDismiss)
    {
        [_overlayView addTarget:self
                         action:@selector(dismiss)
               forControlEvents:UIControlEventTouchUpInside];
    }
    
    [pView addSubview:_overlayView];
    [pView addSubview:self];
    [pView bringSubviewToFront:self];
    
    self.center = CGPointMake(pView.bounds.size.width/2.0f,pView.frame.size.height - self.frame.size.height/2);
    
    
    [self fadeIn];
}

- (void)show
{
    _overlayView = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    _overlayView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.5];
    
    if(self.touchToDismiss)
    {
        [_overlayView addTarget:self
                         action:@selector(dismiss)
               forControlEvents:UIControlEventTouchUpInside];
    }
    
    UIWindow *keywindow = [ELBasePupupView getMainWindow];
    [keywindow addSubview:_overlayView];
    [keywindow addSubview:self];
    [keywindow bringSubviewToFront:self];
    
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height + incressedHeight);
    
    self.center = CGPointMake(keywindow.bounds.size.width/2.0f,keywindow.bounds.size.height/2.0f);
    
    [self fadeIn];
}
- (void)showWithoutAnimation
{
    _overlayView = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    _overlayView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.5];
    
    if(self.touchToDismiss)
    {
        [_overlayView addTarget:self
                         action:@selector(dismiss)
               forControlEvents:UIControlEventTouchUpInside];
    }
    
    UIWindow *keywindow = [ELBasePupupView getMainWindow];
    [keywindow addSubview:_overlayView];
    [keywindow addSubview:self];
    [keywindow bringSubviewToFront:self];
    
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height + incressedHeight);
    self.center = CGPointMake(keywindow.bounds.size.width/2.0f,keywindow.bounds.size.height/2.0f);
    self.alpha = 0.0f;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.35];
    self.alpha = 1.0f;
    [UIView commitAnimations];
}

- (void)showAtBottom
{
    _overlayView = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    _overlayView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.5];
    
    if(self.touchToDismiss)
    {
        [_overlayView addTarget:self
                         action:@selector(dismissFromBottom)
               forControlEvents:UIControlEventTouchUpInside];
    }
    
    UIWindow *keywindow = [ELBasePupupView getMainWindow];
    [keywindow addSubview:_overlayView];
    [keywindow addSubview:self];
    [keywindow bringSubviewToFront:self];
    
    self.center = CGPointMake(keywindow.bounds.size.width/2.0f,keywindow.frame.size.height - self.frame.size.height/2);

    [self fadeInFromBottom:keywindow];
}

- (void)dismiss
{
    if (_dismissAction) {
        _dismissAction();
    }
    if(disableUserInterfaced)
        return;
    [self fadeOut];
}

- (void)dismissFromBottom
{
    if (_dismissAction) {
        _dismissAction();
    }
    if(disableUserInterfaced)
        return;
    [self fadeOutToBottom:[ELBasePupupView getMainWindow]];
    
}


#pragma mark - keyboard notification
- (NSInteger)getKeyBoardHeight:(NSNotification *)notification
{
    NSDictionary* keyboardInfo = [notification userInfo];
    NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
    CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
    NSInteger keyboardHeight = keyboardFrameBeginRect.size.height;
    return keyboardHeight;
}

- (void)keyboardDidShow:(NSNotification *)notification
{
    UIWindow *keywindow = [ELBasePupupView getMainWindow];
    NSInteger keyboardHeight;
    keyboardHeight = [self getKeyBoardHeight:notification];
    NSInteger movement = MAX(0,(self.frame.origin.y+self.frame.size.height + keyboardHeight) - keywindow.bounds.size.height);
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.2];
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        self.center = CGPointMake(self.center.x, self.center.y - movement-2);

        [UIView commitAnimations];
    });
}
-(void)keyboardWillHide:(NSNotification *)notification
{

}




@end
